'use strict';

/**
 * @name VansC2B测试接口
 * @version 1.0.0
 * @author xiech
 * @Time 2020年08月19日16:45:19
 */

// 测试接口
exports.main = async (ctx) => {
    let retObj = {};
    try {
        retObj.aaa = "aaaa";
        retObj.categoryGet = await categoryGet(ctx);

        return await basiCode(0, retObj);
    } catch (e) {
        return await basiCode(e.code || 8000, e);
    }
};



// 获取图片分类信息
var categoryGet = async (ctx) => {
    const { cloud } = ctx;
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.picture.category.get',
            autoSession: true,
            data: {
                picture_category_name: 'VansC2B项目',
                // parent_id: '1699317075791582000',
            }
        });
        try {
            let aaa = JSON.stringify(result.picture_categories.picture_category[0].picture_category_id); 
            console.log(aaa);
        } catch (e) {
            console.log(e);
        }

        return result;

    } catch (err) {
        console.log(err);
    }
};

// 获取用户的订单列表
exports.getSoldInfo = async (ctx) => {
    let retObj = {};
    try {
        retObj.sold = await getSoldInfo(ctx);

        return await basiCode(0, retObj);
    } catch (e) {
        return await basiCode(e.code || 8000, e);
    }
};

// 获取订单详情
exports.getTradeInfo = async (ctx) => {
    let retObj = {};
    try {
        retObj.getTradeInfo = await getTradeInfo(ctx);

        return await basiCode(0, retObj);
    } catch (e) {
        return await basiCode(e.code || 8000, e);
    }
};

// 获取用户订单
var getSoldInfo = async (ctx) => {
    const { cloud, data: { openId = '' } } = ctx;
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.open.trades.sold.get',
            autoSession: true,
            data: {
                'fields': 'tid,type,status,payment,orders,rx_audit_status,buyer_nick',
                'buyer_open_id': openId || ctx.openId
            }
        });
        return result;

    } catch (err) {
        console.log(err);
    }
};

// 获取用户订单详情
var getTradeInfo = async (ctx) => {
    const { cloud, data: { tid = '1626978025850347301' } } = ctx;
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.open.trade.get',
            autoSession: true,
            data: {
                'fields': 'tid,type,status,payment,orders,buyer_nick',
                tid
            }
        });
        return result;

    } catch (err) {
        console.log(err);
    }
};



// 获取订单
exports.getOnsale = async (ctx) => {
    let retObj = {};
    try {
        let obj = await getOnsaleInfo(ctx);
        if (!obj.skus.sku) {
            return await basiCode(7000, retObj);
        } else {
            retObj.obj = obj.skus.sku;
        }
        return await basiCode(0, retObj);
    } catch (e) {
        return await basiCode(e.code || 8000, e);
    }
};

//标记用户
exports.markUser = async (context) => {
    let retObj = {};
    const queryData = context.data
    try {
        retObj.markUser = await context.cloud.topApi.invoke({
            api: 'taobao.opentrade.special.users.mark',
            data: {
                'hit': true,
                'open_user_ids': [context.openId],
                'item_id': +queryData.item_id,
                'sku_id': +queryData.sku_id,
                'status': 'MARK'
            },
            autoSession: true
        });

        return await basiCode(0, retObj);
    } catch (e) {
        return await basiCode(e.code || 8000, e);
    }
};
// 获取订单详情
// var getOnsaleInfo = async (ctx) => {
//     const { cloud } = ctx;
//     try {
//         const result = await cloud.topApi.invoke({
//             api: 'taobao.items.onsale.get',
//             autoSession: true,
//             data: {
//                 'fields': 'approve_status,num_iid,title,nick,type,cid,pic_url,num,props,valid_thru,list_time,price,has_discount,has_invoice,has_warranty,has_showcase,modified,delist_time,postage_id,seller_cids,outer_id,sold_quantity'
//             }
//         });
//         return result;

//     } catch (err) {
//         console.log(err);
//     }
// };


// 获取订单详情
// var getOnsaleInfo = async (ctx) => {
//     const { cloud } = ctx;
//     try {
//         const result = await cloud.topApi.invoke({
//             api: 'taobao.item.skus.get',
//             autoSession: true,
//             data: {
//                 'fields': 'sku_id,num_iid,outer_id,created',
//                 'num_iids': '626247211808'
//             }
//         });
//         return result;

//     } catch (err) {
//         console.log(err);
//     }
// };


// 获取订单详情
// var getOnsaleInfo = async (ctx) => {
//     const { cloud } = ctx;
//     try {
//         const result = await cloud.topApi.invoke({
//             api: 'taobao.item.sku.get',
//             autoSession: true,
//             data: {
//                 'fields': 'sku_id,iid,properties,quantity,price,outer_id,created,modified,status',
//                 'num_iids': '583865501344',
//                 'sku_id': '3937532301017'
//             }
//         });
//         return result;

//     } catch (err) {
//         console.log(err);
//     }
// };


// 获取订单详情
var getOnsaleInfo = async (ctx) => {
    const { cloud, data: { outer_id } } = ctx;
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.skus.custom.get',
            autoSession: true,
            data: {
                'fields': 'sku_id,properties,num_iid,properties_name,price,properties_name,status',
                outer_id
            }
        });
        if (!result.skus) {
            result.skus = {}
        }
        return result;

    } catch (err) {
        console.log(err);
    }
};

// 获取加入会员地址
var getJoinUrl = async (ctx) => {
    const { cloud } = ctx;
    let extraInfo = {
        "source": "isvapp",
        "activityId": "314533972120499",
        "entrance": "MiniProjects_loreal"
    };
    try {
        const response = await cloud.topApi.invoke({
            api: 'taobao.crm.member.joinurl.get',
            autoSession: true,
            data: {
                'callback_url': "",
                'extra_info': JSON.stringify(extraInfo)
            }

        });
        return response.result.result;
    } catch (err) {
        console.log(err);
        return null;
    }
}



// 获取东八区时间
var curentTime = function (time) {
    var d = time ? new Date(time) : new Date();
    var localTime = d.getTime();
    var localOffset = d.getTimezoneOffset() * 60000; //获得当地时间偏移的毫秒数
    var utc = localTime + localOffset + 3600000 * 8;
    var now = new Date(utc);

    var year = now.getFullYear(); //年
    var month = now.getMonth() + 1; //月
    var day = now.getDate(); //日
    var hh = now.getHours(); //时
    var mm = now.getMinutes(); //分
    var ss = now.getSeconds(); //秒

    var clock = year + "-";

    if (month < 10)
        clock += "0";

    clock += month + "-";

    if (day < 10)
        clock += "0";

    clock += day + " ";

    if (hh < 10)
        clock += "0";

    clock += hh + ":";

    if (mm < 10)
        clock += "0";

    clock += mm + ":";

    if (ss < 10)
        clock += "0";

    clock += ss;

    return (clock);
}

// 返回值处理
var basiCode = async (code, data) => {
    let reobj = {
        errCode: 0,
        msg: 'ok',
        data: data
    };
    if (code) {
        reobj.data = data;
        reobj.errCode = code;
        switch (code) {
            case 7000:
                reobj.msg = '没有对应商品';
                break;
            case 8000:
                reobj.data = String(data);
                reobj.msg = 'System error!';
                break;
            default:
                reobj.msg = 'other error!';
                break;
        }
    } else {
        reobj.data = data;
    }
    return reobj;
};